package com.jstyle.jclife.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.blesdk.model.SetPersonalInfo;
import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.adapter.BindDeviceInfoAdapter;
import com.jstyle.jclife.app.MyApplication;
import com.jstyle.jclife.ble.BleManager;
import com.jstyle.jclife.ble.BleService;
import com.jstyle.jclife.daoManager.BindDeviceInfoDaoManager;
import com.jstyle.jclife.daoManager.UserInfoDaoManager;
import com.jstyle.jclife.model.ActionData;
import com.jstyle.jclife.model.BindDeviceInfo;
import com.jstyle.jclife.model.BleData;
import com.jstyle.jclife.model.UserInfo;
import com.jstyle.jclife.utils.RxBus;
import com.jstyle.jclife.utils.SharedPreferenceUtils;
import com.jstyle.jclife.utils.Utils;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class BindDeviceListActivity extends BaseActivity {
    private static final String TAG = "BindDeviceListActivity";
    public static final String ActionDeviceClear = "actionDeviceClear";
    public static final String ActionDeviceChange = "ActionDeviceChange";
    @BindView(R.id.bt_device_title)
    Button btDeviceTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.RecyclerView_bindDeviceInfo)
    RecyclerView RecyclerViewBindDeviceInfo;
    @BindView(R.id.tv_appVersion)
    TextView tvAppVersion;
    private Disposable subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_device);
        ButterKnife.bind(this);
        initData();
    }

    boolean isChange = false;
    String connectAddress;

    private void initData() {

        tvAppVersion.setText(getString(R.string.App_Version) + " V" + MyApplication.getAppVersion(this));
        RxBus.getInstance().toObservable(BleData.class).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<BleData>() {
            @Override
            public void onSubscribe(Disposable d) {
                subscription = d;
            }

            @Override
            public void onNext(BleData bleData) {
                String action = bleData.getAction();
                if (BleService.ACTION_GATT_onDescriptorWrite.equals(action)) {
                    showToast(getString(R.string.connected_success));
                    disMissProgressDialog();
                    showBindDeviceList();
                    //sendData(SendCmdState.READ_VERSION);
                    //finish();
                } else if (BleService.ACTION_GATT_DISCONNECTED.equals(action)) {
                    //showToast(getString(R.string.disonnected));
                    if (isChange) {
                        BleManager.getInstance().connectDevice(connectAddress,true);
                        isChange = false;
                    }
                } else if (ActionDeviceChange.equals(action)) {
                    isChange = true;
                    connectAddress = bleData.getData();
                    showProgressDialog(getString(R.string.Switching_device));
                    showBindDeviceList();
                }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        showBindDeviceList();

    }
    boolean checkVersion=true;
    @Override
    protected void dfuLater() {
        super.dfuLater();
        checkVersion=false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        RxBus.getInstance().sendAction(ActionData.ChangeDevice);
        unSubscribe(subscription);
    }

    BindDeviceInfoAdapter bindDeviceInfoAdapter;
    private void showBindDeviceList() {
        List<BindDeviceInfo> bindDeviceInfoList = BindDeviceInfoDaoManager.queryAllData();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(!TextUtils.isEmpty(address)){
            BindDeviceInfo firstBind=null;
            for(BindDeviceInfo bindDeviceInfo:bindDeviceInfoList){
                if(address.equals(bindDeviceInfo.getMacAddress())){
                    firstBind=bindDeviceInfo;
                }
            }
            if(firstBind!=null){
                bindDeviceInfoList.remove(firstBind);
                bindDeviceInfoList.add(0,firstBind);
            }

        }

        bindDeviceInfoAdapter = new BindDeviceInfoAdapter(bindDeviceInfoList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerViewBindDeviceInfo.setLayoutManager(linearLayoutManager);
        RecyclerViewBindDeviceInfo.setAdapter(bindDeviceInfoAdapter);
        if(checkVersion){
            Log.i(TAG, "showBindDeviceList: checkversion");
            sendData(SendCmdState.READ_VERSION);
            checkVersion=false;
        }

    }

    @OnClick({R.id.iv_back, R.id.bt_skip})
    public void onViewClicked(View view) {
        if(Utils.isFastClick()){return;}
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_skip:
                startActivity(BindDeviceActivity.class);
                break;
        }
    }

    @Override
    public void dataCallback(Map<String, String> maps, SendCmdState sendCmdState) {
        super.dataCallback(maps, sendCmdState);
        switch (sendCmdState) {
            case READ_VERSION:
                String version = maps.get(DeviceKey.KVersion);
                bindDeviceInfoAdapter.updateVersion(version);

               // updateDeviceUserInfo();
                break;
        }
    }


}
